home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2660 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  122 lines

  1. Path: matisse.eecs.uic.edu!dhanley
  2. From: dhanley@matisse.eecs.uic.edu (David Hanley)
  3. Newsgroups: comp.lang.misc,comp.lang.perl.misc,comp.lang.tcl,comp.lang.c,comp.lang.java
  4. Subject: Re: Relative Speed of Perl vs. Tcl vs. C
  5. Followup-To: comp.lang.misc,comp.lang.perl.misc,comp.lang.tcl,comp.lang.c,comp.lang.java
  6. Date: 22 Jan 1996 22:35:44 GMT
  7. Organization: University of Illinois at Chicago
  8. Message-ID: <4e13g0$3ip6@tigger.cc.uic.edu>
  9. References: <4dhuoj$cbe@shellx.best.com> <9602122.8425@mulga.cs.mu.OZ.AU> <ukvim4pceu.fsf@linda.teleport.com>
  10. NNTP-Posting-Host: matisse.eecs.uic.edu
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Randal L. Schwartz (merlyn@stonehenge.com) wrote:
  14. : >>>>> "Tim" == Tim Hollebeek <tim@franck> writes:
  15.  
  16. : Tim> An important point, though, is that all 'reasonably' written C programs
  17. : Tim> will run within a factor of 2 or so of 'about as fast as you can get,
  18. : Tim> while for some problems, Perl won't be able to break through about a
  19. : Tim> factor of 7 or so, even if you are a Perl expert.
  20.  
  21. : I just want to point out to everyone that you are simply making that
  22. : figure up based on what you think Perl is doing internally.
  23.  
  24. : Tom Christiansen has had a long-standing bet that someone can't give
  25. : him a program in C that he can't make run no more than "e" times
  26. : slower (about 2.8 for you non-math-geeks) in Perl.  So far, no one's
  27. : done it.
  28.  
  29.     I find that exceptionally hard to beleive.  What is this bet 
  30. exactly?  Do I get any money for "winning" it?  For example, I have 
  31. some programs that are heavily math-intensive, ( matrix multiplies,
  32. number cracking, image manipulation ) which I have written in C, 
  33. which I would be shocked as all get-out to see run nearly as fast in 
  34. PERL.
  35.  
  36. : That's also been my experience as well.  Perl is *not* slow.  In fact,
  37. : a Perl-written egrep is *faster* than most vendor's egrep, for
  38. : example.
  39.  
  40.     Sure, as long as you are writing grep, it's great.  That's
  41. because you're then spending 99% of your time in some C code that's
  42. already been way optimized.
  43.  
  44. : So, if you have a number of "7-times-slower" programs, post'em, and I
  45. : bet we can easily boost their speed by two or three times.
  46.  
  47.     Okay.  It's a bit long, but this finds amicable number pairs
  48. using the dumb old-try-every-number method.  I'd be shocked to see a
  49. 2.8 factor, and be even mildly surprised to even see 7 times as fast
  50. C->Perl.
  51.  
  52. #include <iostream.h>
  53. #include <math.h>
  54. #include <stdlib.h>
  55. #include <string.h>
  56.  
  57. typedef unsigned int Int;
  58. char *Table;
  59.  
  60. unsigned int SumOfFactors( Int Num )
  61. {
  62.     Int Res = 1;
  63.     Int SNum = Int( floor( sqrt( (double) Num ) ) );
  64.     memset( Table , 0 , SNum );
  65.     for( Int i = 2 ; i < SNum ; ++ i )
  66.     {
  67.       if ( Table[ i ] == 0 )
  68.       {
  69.         int divresult = Num / i;
  70.         int Remainder = Num - ( divresult * i ) ;
  71.         if ( Remainder == 0 )
  72.     {
  73.       Res += i;
  74.       Res += divresult;
  75.     }
  76.         else
  77.         {
  78.           int temp = i;
  79.           while( temp < SNum )
  80.           {
  81.             Table[ temp ] = 1;
  82.             temp += i;
  83.           }
  84.         }
  85.       }
  86.     }
  87.     return Res;
  88. }
  89.  
  90. main( int argc , char *argv[] )
  91. {
  92.     Table = new char[ 1000000 ];
  93.  
  94.     for( Int i = 1 ; i < 1000000000 ; ++i )
  95.     {
  96.         if ( ( i % 100000 ) == 0 )
  97.         {
  98.             cout << "Reached " << i << endl;
  99.         }
  100.         Int r1 = SumOfFactors( i );
  101.         if ( r1 == i )
  102.             cout << "Perfect " << i << endl;
  103.         else
  104.         {
  105.             Int r2 = SumOfFactors( r1 );
  106.             if ( r2 == i )
  107.                 cout << "Amicable : " << r1 << " " << r2 << endl;
  108.         }
  109.     }
  110. }
  111.         
  112.  
  113.  
  114. --
  115. ------------------------------------------------------------------------------
  116. David Hanley,                 |______     Computer Science graduate student.  
  117. dhanley@lac.ecs.uic.edu       |\ ___/__  Martial Artist. Biker. Chess Freak   
  118. www_lac.eecs.uic.edu/~dhanley/| \\ /  / Libertarian.  Atheist.  Bisexual.     
  119. My employer barely KNOWS me.  |  \/BI/ Aspiring novelist.Joyce Kafka Neitzchie
  120. -----------------------------------\/-----------------------------------------
  121. Member:Hermetic order of the Golden Dawn.
  122.